//
//  SmallDecisionsZxhOrDmxInsertPeopleViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsZxhOrDmxInsertPeopleViewController.h"
#import "SmallDecisionsPlaceholdTextView.h"

@interface SmallDecisionsZxhOrDmxInsertPeopleViewController ()
@property (nonatomic, strong) SmallDecisionsPlaceholdTextView *insertPropleTextView;
@property (nonatomic, strong) UIButton *insertPropleSureBtn;

@end

@implementation SmallDecisionsZxhOrDmxInsertPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加选项";
    [self insertPropleTextView];
    [self insertPropleSureBtn];
}

- (SmallDecisionsPlaceholdTextView *)insertPropleTextView {
    if(!_insertPropleTextView) {
        _insertPropleTextView = [[SmallDecisionsPlaceholdTextView alloc] init];
        _insertPropleTextView.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15);
        _insertPropleTextView.backgroundColor = SmallDecisionsWhiteColor;
        _insertPropleTextView.layer.cornerRadius = 6;
        _insertPropleTextView.layer.masksToBounds = YES;
        _insertPropleTextView.layer.borderColor = SmallDecisionsThemeColor.CGColor;
        _insertPropleTextView.layer.borderWidth = 1;
        if(self.list.count) {
            _insertPropleTextView.text = [self.list componentsJoinedByString:@"\n"];
        }
        _insertPropleTextView.textColor = SmallDecisionsBlackColor;
        _insertPropleTextView.font =  SmallDecisionsSetFontSize(14);
        _insertPropleTextView.returnKeyType = UIReturnKeyDone;
        _insertPropleTextView.placeholder = @"请输入玩家，每行一个，例如：\n李白\n杜甫\n孟浩然\n王维";
        _insertPropleTextView.placeholderColor = SmallDecisionsLineColor;
        [self.view addSubview:_insertPropleTextView];
        [_insertPropleTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(40);
            make.right.mas_equalTo(-40);
            make.height.mas_equalTo(300);
            make.centerY.mas_equalTo(-10);
        }];
    }
    return _insertPropleTextView;
}

- (UIButton *)insertPropleSureBtn {
    if(!_insertPropleSureBtn) {
        _insertPropleSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_insertPropleSureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_insertPropleSureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_insertPropleSureBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _insertPropleSureBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(16);
        _insertPropleSureBtn.layer.cornerRadius = 25;
        _insertPropleSureBtn.layer.masksToBounds = YES;
        _insertPropleSureBtn.backgroundColor = SmallDecisionsThemeColor;
        [_insertPropleSureBtn addTarget:self action:@selector(insertPropleSureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_insertPropleSureBtn];
        [_insertPropleSureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.left.right.equalTo(self.insertPropleTextView);
            make.top.equalTo(self.insertPropleTextView.mas_bottom).offset(20);
        }];
    }
    return _insertPropleSureBtn;
}

- (void)insertPropleSureBtnAction {
    NSString *peopleNames = self.insertPropleTextView.text;
    peopleNames = [peopleNames stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *names = [peopleNames componentsSeparatedByString:@"\n"];
    if (names.count > 2) {
        if (self.callback) {
            self.callback(names);
        }
    }
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:YES completion:nil];
}

@end
